package com.ruoyi.yljf.service.impl;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.yljf.domain.Course;
import com.ruoyi.yljf.domain.Question;
import com.ruoyi.yljf.domain.TestPaper;
import com.ruoyi.yljf.domain.vo.adminVO.PaperTaxonVO;
import com.ruoyi.yljf.domain.vo.adminVO.TestPaperAdminVO;
import com.ruoyi.yljf.mapper.CourseMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.yljf.mapper.PaperTaxonMapper;
import com.ruoyi.yljf.domain.PaperTaxon;
import com.ruoyi.yljf.service.IPaperTaxonService;

import javax.annotation.Resource;

/**
 * 课程试卷列表Service业务层处理
 * 
 * @author metaee
 * @date 2025-04-25
 */
@Service
public class PaperTaxonServiceImpl extends ServiceImpl<PaperTaxonMapper, PaperTaxon> implements IPaperTaxonService {
    @Resource
    private PaperTaxonMapper paperTaxonMapper;
     @Resource
    private CourseMapper courseMapper;

    /**
     * 查询课程试卷列表列表
     * 
     * @param paperTaxon 课程试卷列表
     * @return 课程试卷列表
     */
    @Override
    public List<PaperTaxon> selectPaperTaxonList(PaperTaxon paperTaxon) {
    	LambdaQueryWrapper<PaperTaxon> queryWrapper = Wrappers.lambdaQuery();
        if (StringUtils.isNotEmpty(paperTaxon.getTaxonName())) {
			queryWrapper.like(PaperTaxon::getTaxonName, paperTaxon.getTaxonName());
		}
        if (StringUtils.isNotNull(paperTaxon.getCourseId())) {
			queryWrapper.eq(PaperTaxon::getCourseId, paperTaxon.getCourseId());
		}
        return paperTaxonMapper.selectList(queryWrapper);
    }

    @Override
    public List<PaperTaxonVO> handlePaperTaxon(List<PaperTaxon> list) {
        Set<Long> quesIds = list.stream()
                .map(PaperTaxon::getCourseId)
                .filter(Objects::nonNull)
                .collect(Collectors.toSet());
        List<Course> courses = courseMapper.selectBatchIds(quesIds);
        // 3. 将 Question 转换为 Map<ID, Question> 以便快速查找
        Map<Long, String> curseMap = courses.stream()
                .collect(Collectors.toMap(Course::getId,  Course::getName));

        // 4. 遍历 list 并关联赋值
        List<PaperTaxonVO> result = list.stream()
                .map(paperTaxon -> {
                    PaperTaxonVO vo = new PaperTaxonVO();
                    // 复制原有属性（假设有 BeanUtils 或手动 set）
                    BeanUtils.copyProperties(paperTaxon,  vo);

                    // 通过 courseQuestionId 匹配并赋值 Question
                    if (paperTaxon.getCourseId()  != null) {
                        vo.setCourseName(curseMap.get(paperTaxon.getCourseId()));
                    }


                    return vo;
                })
                .collect(Collectors.toList());
    return result;
    }
}
